package com.shycloud.mido.member.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.member.api.dto.MemberRoleDTO;
import com.shycloud.mido.member.api.entity.Member;
import com.shycloud.mido.member.api.entity.MemberRole;
import com.shycloud.mido.member.api.entity.Role;
import com.shycloud.mido.member.api.util.Constant;
import com.shycloud.mido.member.api.vo.MembershipInfoVO;
import com.shycloud.mido.member.mapper.MemberMapper;
import com.shycloud.mido.member.mapper.MemberRoleMapper;
import com.shycloud.mido.member.service.MemberRoleService;

import lombok.AllArgsConstructor;

/**
 * 会员角色实现
 *  @author yyy
 *  @date 2019/05/20
 */
@Service
@AllArgsConstructor
public class MemberRoleServiceImpl extends ServiceImpl<MemberRoleMapper, MemberRole> implements MemberRoleService {

	private MemberRoleMapper memberRoleMapper;
	private MemberMapper memberMapper;
	/**
	 *
	 * 查询会员角色
	 * @param memberId 会员ID
	 * @return
	 */
	@Override
	public List<Role> memberRole(String memberId){
		return memberRoleMapper.memberRole(memberId);
	}

	/**
	 * 查询用户身份
	 * @param memberId 会员ID
	 * @return
	 */
	@Override
	public List<MemberRole> getRole(String memberId){
		return memberRoleMapper.getRole(memberId);
	}


	/**
	 * 新增会员角色
	 * @param memberRole
	 */
	@Override
	public void saveMemberRole(MemberRole memberRole){
		 memberRoleMapper.saveMemberRole(memberRole);
	}

	/**
	 * 更新会员角色
	 * @param memberRole
	 */
	@Override
	public void updMemberRole(MemberRole memberRole){
		memberRoleMapper.updMemberRole(memberRole);
	}


	/**
	 * 查找会员到期时间
	 * @param memberId 会员Id
	 * @param roleName 	 角色名称
	 * @return LocalDateTime
	 */
	@Override
	public LocalDateTime getEndTime(String memberId, String roleName){
		return memberRoleMapper.getEndTime(memberId,roleName);
	}

	/**
	 * 查询会员角色信息
	 *
	 * @param memberId 会员ID
	 * @return
	 */
	@Override
	public MembershipInfoVO selMemberRole(String memberId){
		List<MembershipInfoVO> list =memberRoleMapper.selMemberRole(memberId);
		//高级
		MembershipInfoVO vo;
		if(list.size()>0){
			vo = new MembershipInfoVO();
			for(int i = 0; i < list.size();i++){
				vo = list.get(i);
				if(list.size()>1 && list.get(i).getRoleName().equals(Constant.VIP_MEMBER)){
					return vo;
				}
			}
			return  vo;
		}else{
			vo= new MembershipInfoVO();
			Member m =memberMapper.selectById(memberId);
			if(m.getAvatar()!=null){
				vo.setAvatar(m.getAvatar());
			}
			if(m.getPhone()!=null){
				vo.setPhone(m.getPhone());
			}
			if(m.getShowname()!=null){
				vo.setShowname(m.getShowname());
			}
			vo.setMemberId(memberId);
			vo.setEndTime(null);
			vo.setRoleName("注册会员");
			return vo;
		}
	}

	/**
	 * 离会员到期1个月
	 * 离会员到期7天
	 * 离会员到期3天
	 * de 所有会员
	 * @return
	 */
	@Override
	public List<MemberRoleDTO> roleExpire(){
		//离会员到期7天
		LocalDateTime seven = LocalDateTime.now().plusDays(7L);
		//离会员到期3天
		LocalDateTime three =LocalDateTime.now().plusDays(3L);
		List<MemberRoleDTO> list = memberRoleMapper.roleExpire(seven,three);
		for(int i =0; i<list.size(); i++){
			Member member = memberMapper.selectById(list.get(i).getMemberId());
			list.get(i).setMemberName(member.getShowname());
			if(StringUtils.isNotEmpty(member.getClientId())){
				list.get(i).setCid(member.getClientId());
			}
			if(list.get(i).getRoleCode().equals(Constant.VIP_MEMBER)){
				list.get(i).setRoleName("高级会员");
			}else{
				list.get(i).setRoleName("普通会员");
			}
		}

		return list;
	}


	/**
	 * 会员过期
	 * @return
	 */
	@Override
	public List<MemberRoleDTO>roleOverdue(){
		//开始查询过期会员的事件节点
		LocalDateTime start = LocalDateTime.now().plusDays(-1);
		//结束查询过期会员的时间节点
		LocalDateTime end = LocalDateTime.now().plusDays(1);
		List<MemberRoleDTO> list  = memberRoleMapper.roleOverdue(start,end);
		for(int i =0; i<list.size(); i++){
			Member member = memberMapper.selectById(list.get(i).getMemberId());
			list.get(i).setMemberName(member.getShowname());
			if(StringUtils.isNotEmpty(member.getClientId())){
				list.get(i).setCid(member.getClientId());
			}
			if(list.get(i).getRoleCode().equals(Constant.VIP_MEMBER)){
				list.get(i).setRoleName("高级会员");
			}else{
				list.get(i).setRoleName("普通会员");
			}
		}

		return list;
	}

}
